home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS03.ADF / Xref / getch.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  1KB  |  64 lines

  1. /**************************************************************************
  2.  
  3. NAME
  4.  
  5.      getch/ungetch -- buffered use of getchar allowing "push-back"
  6.  
  7. SYNOPSIS
  8.  
  9.      c = getch();   get character from file
  10.      int c;         next input character or EOF 
  11.  
  12.      ungetch(c);    push character back on input
  13.      int c;         character to be output
  14.  
  15.  
  16. DESCRIPTION
  17.  
  18.      Use getch in lieu of getchar when it is desirable to reread an
  19.      unwanted character later.  Use ungetch to "push back" the
  20.      unwanted character for the next use of getch.
  21.  
  22. RETURNS
  23.  
  24.      c = character
  25.        = EOF if end-of-file or error
  26.  
  27. CAUTIONS
  28.  
  29.      Buffer will hold only 100 characters.
  30.  
  31. AUTHOR    Philip T. Ansteth
  32. DATE      Nov. 19, 1985
  33. CLIENT    ANSTETH RESEARCH
  34.  
  35. CALLED FUNCTIONS
  36.  
  37.      getchar   get character from file
  38.  
  39.  
  40. UPDATE HISTORY
  41.  
  42.  
  43. **************************************************************************/
  44.  
  45. #include <stdio.h>
  46. #define BUFSIZE 100
  47. char buf[BUFSIZE];  /* buffer for ungetch */
  48. int bufp = 0;       /* next free position in buf */
  49.  
  50. getch()             /* get a (possibly pushed back) character */
  51. {
  52.      return((bufp > 0) ? buf[--bufp] : getchar());
  53. }
  54.  
  55. ungetch(c)          /* push character back on input */
  56. int c;
  57. {
  58.      if (bufp > BUFSIZE)
  59.           printf("ungetch:  too many characters\n");
  60.      else
  61.           buf[bufp++] = c;
  62. }
  63.  
  64.